
/**
* @Title: Student.java
* @Package com.leetone
* @Description: 【】
* @date 2018年9月1日 下午1:44:20
* @version V1.0
* @Company: 上海未泰信息技术有限公司
* @author:[谭磊][leetone123@163.com]
*/

package com.leetone;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
* @ClassName: Student【延时队列实例】
* @Description: 【作为DelayQueue的元素必须实现该接口的两个方法】
* @date 2018年9月1日 下午1:44:20
* @Company: 上海未泰信息技术有限公司
* @author:[谭磊][leetone123@163.com]
*/
//1.必须实现Delayed接口
public class Student implements Delayed{
	private String name;
	//交卷时间
	private long submitTime;
	//考试时间
	private long workTime;
	public String getName() {
		return this.name +"交卷，考试时间:\t"+workTime;
	}
	public Student(String name,long submitTime) {
		this.name = name;
		this.workTime = submitTime;
		this.submitTime = TimeUnit.NANOSECONDS.convert(submitTime, TimeUnit.MILLISECONDS) + System.nanoTime();
		System.out.println(this.name +"交卷，用时:\t"+workTime);
	}
	//必须实现下面2个方法
	@Override
	public long getDelay(TimeUnit unit) {
		// 返回一个延时时间
		return unit.convert(submitTime - System.nanoTime(), unit.NANOSECONDS);
	}
	@Override
	public int compareTo(Delayed o) {
		// 比较的方法(包括大于等于和小于)
		Student that = (Student) o;
		return submitTime > that.submitTime ? 1 : (submitTime < that.submitTime ? -1 : 0); 
	}
}
